HiveMQ Enterprise Extension for MongoDB
MongoDB is an open-source, document-oriented database system. MongoDB is designed for scalability, high availability, and performance which makes it suitable for various types of applications, including big data, analytics, and IoT.
Unlike traditional relational databases that use tables, rows, and columns to store data, MongoDB uses a JSON-like format called BSON (Binary JSON) to store documents. These documents are organized in collections, that are analogous to the tables in a relational database.
Features
-
Forward MQTT messages from IoT devices to one or more MongoDB databases via your HiveMQ broker.
-
Convert MQTT messages into MongoDB documents with a flexible templating system.
Requirements
-
A running HiveMQ Professional or Enterprise Edition installation, version 4.17 or higher.
-
A running MongoDB instance, version 4.4 or higher is recommended.
-
For production use, a valid HiveMQ Enterprise Extension for MongoDB license.
If you do not provide a valid license, HiveMQ automatically uses a free trial license. Trial licenses for HiveMQ Enterprise Extensions are valid for 5 hours. For more license information or to request an extended evaluation license, contact HiveMQ sales. |
Installation
-
Place your HiveMQ Enterprise Extension for MongoDB license file (.elic) in the license folder of your HiveMQ installation. (Skip this step if you are using a trial version of the extension).
└─ <HiveMQ folder> ├─ bin ├─ conf ├─ data ├─ extensions │ ├─ hivemq-mongodb-extension │ └─ ... ├─ license ├─ log └─ ...
-
Before you enable the extension, you need to configure the extension to match your individual MongoDB setup.
For your convenience, we provide an example configurationconf/examples/config.xml
that you can copy and modify as desired.
The includedconfig.xsd
file outlines the schema and elements that can be used in the XML configuration.
Your completed configuration file must be namedconfig.xml
and located inHIVEMQ_HOME/extensions/hivemq-mongodb-extension/conf/config.xml
.
For detailed information on configuration options, see Configuration. -
To enable the HiveMQ Enterprise Extension for MongoDB, locate the
hivemq-mongodb-extension
folder in theextensions
directory of your HiveMQ installation and remove theDISABLED
file (if present).
To function properly, the HiveMQ Enterprise Extension for MongoDB must be installed on all HiveMQ broker nodes in your HiveMQ cluster and the configuration file on each node must be identical. |
Configuration
The extension configuration is divided into two sections:
-
MongoDBs: Provides information about the MongoDB instances to which your HiveMQ broker connects.
-
MQTT to MongoDB Routes: Defines how MQTT messages are sent from your HiveMQ broker to the configured MongoDB instances.
Extension Configuration File
The config.xml
file for your MongoDB extension must be located in the hivemq-mongodb-extension/conf
folder within the extensions
folder of your HiveMQ installation.
The extension uses a simple but powerful XML-based configuration.
The conf/examples/config.xml
file is a configuration example that has all the parameters you need to send MQTT messages from your HiveMQ MQTT broker to MongoDB.
If you copy and reuse the conf/examples/config.xml file, be sure to move the file to /conf/config.xml before you enable your extension.
For more information, see Installation.
|
<hivemq-mongodb-extension xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="config.xsd">
<mongodbs>
<mongodb>
<id>my-mongodb-id</id>
<connection>
<host>my.host.com</host>
<port>27017</port>
</connection>
</mongodb>
</mongodbs>
<mqtt-to-mongodb-routes>
<mqtt-to-mongodb-route>
<id>my-mqtt-to-mongodb-route</id>
<mongodb-id>my-mongodb-id</mongodb-id>
<mqtt-topic-filters>
<mqtt-topic-filter>#</mqtt-topic-filter>
</mqtt-topic-filters>
<database>my-database</database>
<collection>my-collection</collection>
<processor>
<document-template>path/to/my/document-template.json</document-template>
</processor>
</mqtt-to-mongodb-route>
</mqtt-to-mongodb-routes>
</hivemq-mongodb-extension>
MongoDB Databases Configuration
The <mongodbs>
section of your configuration lists the MongoDB databases to which configured MQTT messages are routed.
You can define as many <mongodb>
tags as your use case requires.
<mongodb>
<id>my-mongodb-id</id>
<connection>
<host>my.host.com</host>
<port>27017</port>
</connection>
</mongodb>
<mongodb>
<id>my-mongodb-id</id>
<connection>
<host>my.host.com</host>
<port>27017</port>
<authentication>
<basic>
<username>example-user</username>
<password>example-password</password>
</basic>
</authentication>
</connection>
</mongodb>
Basic authentication expects the user credentials to be in the 'admin' authentication database (default). The authentication mechanism for MongoDB version 4.0 or higher is SCRAM-SHA-256. |
<mongodbs>
<mongodb>
<id>my-mongodb-id</id>
<connection-string>mongodb://example-user:example-password@my-host.com:27017/</connection-string>
</mongodb>
</mongodbs>
<mongodbs>
<mongodb>
<id>my-mongodb-id</id>
<connection-string>mongodb+srv://example-user:example-password@my.dns-srv-record.com/</connection-string>
</mongodb>
</mongodbs>
<mongodb>
<id>my-mongodb-id</id>
<connection-string>mongodb://example-user:example-password@my-host.com:27017/</connection-string>
<tls>
<keystore>
<path>/keystore.jks</path>
<private-key-password>my-private-key-password</private-key-password>
<password>my-keystore-password</password>
</keystore>
<truststore>
<path>/truststore.jks</path>
<password>my-truststore-password</password>
</truststore>
<verifyHostname>false</verifyHostname>
</tls>
</mongodb>
You can use Environment variables to map properties such as passwords or connection strings. |
Parameter | Required | Type | Description |
---|---|---|---|
|
ID |
The ID of the MongoDB instance. This string can only contain lowercase alphanumeric characters, dashes, and underscores. |
|
|
Complex |
The connection information for the MongoDB instance. NOTE: Your configuration must contain either a
|
|
|
- |
String |
A connection string for the MongoDB instance. |
|
- |
Complex |
Defines the type of authentication the extension uses for the MongoDB connection. By default, no values are set.
|
|
- |
Complex |
Optional TLS configuration to establish a secure connection to MongoDB.
|
MQTT to MongoDB Routes
The <mqtt-to-mongodb-routes>
section of your extension configuration defines how MQTT messages are sent from the HiveMQ broker to MongoDB.
You can define as many <mqtt-to-mongodb-route>
tags as your use case requires.
<mqtt-to-mongodb-routes>
<mqtt-to-mongodb-route>
<id>my-mqtt-to-mongodb-route</id>
<mongodb-id>my-mongodb-id</mongodb-id>
<mqtt-topic-filters>
<mqtt-topic-filter>myhome/groundfloor/+/temperature</mqtt-topic-filter>
<mqtt-topic-filter>myhome/firstfloor/#</mqtt-topic-filter>
<mqtt-topic-filter>myhome/secondfloor/bedroom/temperature</mqtt-topic-filter>
</mqtt-topic-filters>
<database>my-database</database>
<collection>my-collection</collection>
<processor>
<document-template>path/to/my/document-template.json</document-template>
</processor>
</mqtt-to-mongodb-route>
</mqtt-to-mongodb-routes>
Parameter | Required | Type | Description |
---|---|---|---|
|
ID |
The ID of the |
|
|
IDREF |
The ID of the MongoDB instance. |
|
|
- |
Boolean |
Optional setting that defines whether the selected |
|
Complex |
A list of one or more MQTT topic filters.
|
|
|
String |
The MongoDB database the MQTT messages are routed to. |
|
|
String |
The MongoDB collection the MQTT messages are routed to. |
|
|
Complex |
Defines how MQTT messages are converted into MongoDB documents.
|
Document Templates
Document templates are JSON files that help you convert MQTT messages into MongoDB documents. These templates allow you to set placeholders that your HiveMQ extension later fills with the properties of the MQTT PUBLISH.
Placeholders start with ${
and end with }
.
{
"topic": "${mqtt-topic}",
"payload_utf8": "${mqtt-payload-utf8}",
"qos": "${mqtt-qos}",
"retain": ${mqtt-retain},
"packet_id": ${mqtt-packet-id},
"payload_format_indicator": "${mqtt-payload-format-indicator}",
"response_topic": "${mqtt-response-topic}",
"correlation_data_utf8": "${mqtt-correlation-data-utf8}",
"user_properties": ${mqtt-user-properties-json},
"my_user_property": "${mqtt-user-properties$myUserPropertyName}",
"arrival_timestamp": ${timestamp-ms}
}
{
"packet_id": ${mqtt-packet-id},
"payload_utf8": ${mqtt-payload-utf8}
}
If you expect JSON payloads, check that the ${mqtt-payload-utf8} placeholder is not surrounded with double quotes to ensure that MongoDB interprets the payload content as JSON rather than String.
|
{
"packet_id": ${mqtt-packet-id},
"arrival_timestamp": ISODate("${timestamp-iso-8601}")
}
Although this example is not a valid JSON, the example works since the extension replaces placeholders such as mqtt-packet-id .
In the example, the placeholder is replaced with a number and MongoDB interprets the ISODate() function.
|
Your HiveMQ extension automatically checks for the presence of unknown placeholders in your document template. If an unknown placeholder is present, HiveMQ logs an error and the HiveMQ Extension for MongoDB does not start. |
The following table lists all placeholders the HiveMQ MongoDB extension recognizes:
Name | Optional properties | Description |
---|---|---|
|
- |
The topic of the MQTT PUBLISH. |
|
The payload of the MQTT PUBLISH as a UTF-8 string. |
|
|
The payload of the MQTT PUBLISH as a Base64 string. |
|
|
- |
The QoS level of the MQTT PUBLISH (AT_MOST_ONCE, AT_LEAST_ONCE, EXACTLY_ONCE). |
|
- |
The retain message flag of the MQTT PUBLISH. |
|
- |
The packet id of the MQTT PUBLISH. |
|
The payload format indicator of the MQTT PUBLISH. |
|
|
The response topic of the MQTT PUBLISH. |
|
|
The correlation data of the MQTT PUBLISH as a UTF-8 string. |
|
|
The correlation data of the MQTT PUBLISH as a Base64 string. |
|
|
The user properties of the MQTT PUBLISH as a JSON array string. |
|
|
The value of the user property of the MQTT PUBLISH with the matching property name. Example: |
|
|
- |
The arrival timestamp of the PUBLISH message represented as a UNIX timestamp value in milliseconds.
|
|
- |
The same information as |
Some properties in an MQTT PUBLISH message are optional. The number of placeholders the MongoDB extension fills varies based on the properties that are present in the MQTT PUBLISH message. Optional properties in the document template are replaced with an empty string if the property is not present in the MQTT PUBLISH message. |
Environment variables
HiveMQ offers placeholders that can be replaced with the content of environment variables when the configuration file is read. For many use cases, it can be beneficial or necessary to use environment variables to configure items such as ports and bind addresses on the system on which you run HiveMQ. For example, when you run HiveMQ in a containerized environment.
You can use ${YOUR_ENVVAR_NAME}
in the config.xml
file.
HiveMQ replaces the placeholder with the value of the specified environment variable during startup.
export MY_CONNECTION_STRING=mongodb://example-user:example-password@my-host.com:27017/
<hivemq-mongodb-extension xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="config.xsd">
<mongodbs>
<mongodb>
<id>my-mongodb-id</id>
<connection-string>${MY_CONNECTION_STRING}</connection-string>
</mongodb>
</mongodbs>
</hivemq-mongodb-extension>
<hivemq-mongodb-extension xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="config.xsd">
<mongodbs>
<mongodb>
<id>my-mongodb-id</id>
<connection-string>mongodb://example-user:example-password@my-host.com:27017/</connection-string>
</mongodb>
</mongodbs>
</hivemq-mongodb-extension>
Make sure that HiveMQ is started in the same context as your environment variables are set, otherwise HiveMQ will not be able to access them. |